package com.kcl.dfss.fragments;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kcl.dfss.DeviceSetActivity;
import com.kcl.dfss.LocationService;
import com.kcl.dfss.LoginActivity;
import com.kcl.dfss.PersonalAccountActivity;
import com.kcl.dfss.PersonalSetActivity;
import com.kcl.dfss.R;
import com.kcl.dfss.RegisterActivity;
import com.kcl.dfss.utils.AppUtils;

public class PersonalFragment extends Fragment {

	private Button btn_login,btn_register;
	private String TAG = "PersonalFragment";
	private Bitmap bitmap = null;
	private SharedPreferences preferences;
	private ImageView personal_head;
	private TextView text_username;
	private String username = null;
	private boolean cameraConnect = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		SharedPreferences loginset = getActivity().getSharedPreferences("shared_login", 0);
		boolean login_state = loginset.getBoolean("login", false);
		username = loginset.getString("usernick","");
		if(login_state){
			View view = inflater.inflate(R.layout.layout_personal_afterlogin, container, false);
			ImageView image_set = (ImageView) view.findViewById(R.id.image_set);
			personal_head = (ImageView) view.findViewById(R.id.personal_head);
			ImageView image_device = (ImageView) view.findViewById(R.id.image_device);
			text_username = (TextView) view.findViewById(R.id.text_username);
			cameraConnect = LocationService.getCamemraConnected();
//			text_username.setText(username);
			
			final Handler handler = new Handler(){

				@Override
				public void handleMessage(Message msg) {
					if(msg.what == 1){
						text_username.setText(username);
						personal_head.setImageBitmap(bitmap);
					}
				}
			};
			
			preferences = getActivity().getSharedPreferences("shared_login", 0);
			
			final String url = preferences.getString("userhead", null);
			if(AppUtils.isNetworkAvailable(getActivity())){
				if(url!=null){
					if(url.equals("http://121.40.142.208/AppServer")){
						if(cameraConnect){
							text_username.setText(username);
						}else{
							text_username.setText("username");						
						}
					}else{
						if(cameraConnect){
							text_username.setText(username);
						}else{
							text_username.setText("头像加载中...");	
							new Thread(new Runnable() {
								@Override
								public void run() {
									bitmap = AppUtils.getHttpBitmap(url);
									if(bitmap!=null){
										Message message = new Message();
										message.what = 1;
										handler.sendMessage(message);								
									}
								}
							}).start();
						}
					}
					/*if(cameraConnect){
						text_username.setText(username);
					}else{
						text_username.setText("头像加载中...");						
					}*/
					/*new Thread(new Runnable() {
						@Override
						public void run() {
							bitmap = AppUtils.getHttpBitmap(url);
							if(bitmap!=null){
								Message message = new Message();
								message.what = 1;
								handler.sendMessage(message);								
							}
						}
					}).start();*/
				}	
			}
			
			image_set.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), PersonalSetActivity.class);
					startActivity(intent);
				}
			});
			personal_head.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), PersonalAccountActivity.class);
					startActivity(intent);
				}
			});
			image_device.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), DeviceSetActivity.class);
					startActivity(intent);
				}
			});
			return view;
		}else{
			View view = inflater.inflate(R.layout.layout_personal,container,false);
			
			btn_login = (Button) view.findViewById(R.id.btn_login);
			btn_register = (Button) view.findViewById(R.id.btn_register);
			
			btn_login.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), LoginActivity.class);
//					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
					startActivity(intent);
				}
			});
			
			btn_register.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(), RegisterActivity.class);
					startActivity(intent);
				}
			});
			return view;
		}
	}


	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG,"onResume");
		preferences = getActivity().getSharedPreferences("shared_login", 0);
		boolean newhead = preferences.getBoolean("newhead", false);
		if(newhead){
			final String url = preferences.getString("userhead", null);
			if(url!=null){
				new Thread(new Runnable() {
					@Override
					public void run() {
						bitmap = AppUtils.getHttpBitmap(url);
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								personal_head.setImageBitmap(bitmap);
							}
						});
						/*if(bitmap!=null){
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);								
						}*/
					}
				}).start();
			}
			preferences.edit().putBoolean("newhead", false).commit();
		}
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
/*	public Bitmap getHttpBitmap(String url){
		URL myUrl;
		Bitmap bitmap = null;
		try {
			myUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
			conn.setConnectTimeout(10000);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}*/
}
